package com.crrc.sandameter

import android.app.Activity
import android.app.Dialog
import android.app.ProgressDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.cj.mvvmlib.ext.util.dp2px

/**
 * dialog工具类
 */
object DialogUtils {


    /**
     * 底部弹窗
     * @param layoutId 自定义布局id
     * @param right 弹框右边距（距离屏幕左边）
     * @param left 弹框左边距
     */
    fun showBottomDialog(context: Context, layoutId: Int, left: Int = 0, right: Int = 0): Dialog {
        val dialog = Dialog(context)
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(layoutId, null, false)
        dialog.apply {
            setContentView(view)
            window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            window?.decorView?.setPadding(context.dp2px(left), 0, context.dp2px(right), 0) //消除边框
            //            window?.setWindowAnimations(R.style.Animation_CustomPopup)
            setCanceledOnTouchOutside(true)
            setCancelable(true)
            window?.setGravity(Gravity.BOTTOM)
        }
        if (context is Activity && !context.isFinishing) {
            dialog.show()
        }
        return dialog
    }

    /**
     * 通用弹框
     * @param context 上下文
     * @param result 结果
     */
    fun showDialog(context: Context,
        carNum: String,
        position: String,
        result: String,
        callback: (Unit) -> Unit,
        cancelCallback: ((Unit) -> Unit)? = null): Dialog {
        val dialog = Dialog(context)
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(R.layout.layout_dialog, null, false)
        val confirmBt = view.findViewById<Button>(R.id.confirm_bt)
        val carNumTv = view.findViewById<TextView>(R.id.car_num_tv)
        val positionTv = view.findViewById<TextView>(R.id.position_tv)
        val resultTv = view.findViewById<TextView>(R.id.result_tv)
        val closeIv = view.findViewById<ImageView>(R.id.close_iv)
        dialog.apply {
            setContentView(view)
            window?.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //            window?.decorView?.setPadding(dp2px(left, context), 0, dp2px(right, context), 0)//消除边框
            //            window?.setWindowAnimations(R.style.Animation_CustomPopup)
            setCanceledOnTouchOutside(true)
            setCancelable(true)
            window?.setGravity(Gravity.CENTER)
        }
        carNumTv.text = carNum
        positionTv.text = position
        resultTv.text = result
        confirmBt.setOnClickListener {
            callback.invoke(Unit)
            dialog.dismiss()
        }
        closeIv.setOnClickListener {
            dialog.dismiss()
        }
        if (context is Activity && !context.isFinishing) {
            dialog.show()
        }
        return dialog
    }

    /**
     * 底部弹窗
     * @param layoutId 自定义布局id
     * @param right 弹框右边距（距离屏幕左边）
     * @param left 弹框左边距
     */
    fun showDiyDialog(context: Context,
        layoutId: Int,
        left: Int = 0,
        right: Int = 0,
        width: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
        height: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
        gravity: Int = Gravity.CENTER): Dialog {
        val dialog = Dialog(context)
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(layoutId, null, false)
        dialog.apply {
            setContentView(view)
            window?.setLayout(width, height)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            window?.decorView?.setPadding(context.dp2px(left), 0, context.dp2px(right), 0) //消除边框
            //            window?.setWindowAnimations(R.style.Animation_CustomPopup)
            setCanceledOnTouchOutside(true)
            setCancelable(true)
            window?.setGravity(gravity)
        }
        if (context is Activity && !context.isFinishing) {
            dialog.show()
        }
        return dialog
    }

    /**
     * 底部弹窗
     * @param layoutId 自定义布局id
     * @param right 弹框右边距（距离屏幕左边）
     * @param left 弹框左边距
     */
    fun showDiyDialog2(context: Context,
        layoutId: Int,
        left: Int = 0,
        right: Int = 0,
        width: Int = ViewGroup.LayoutParams.MATCH_PARENT,
        height: Int = ViewGroup.LayoutParams.MATCH_PARENT,
        gravity: Int = Gravity.CENTER): Dialog {
        val dialog = Dialog(context)
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(layoutId, null, false)
        view.setOnClickListener { dialog.dismiss() }
        dialog.apply {
            setContentView(view)
            window?.setLayout(width, height)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            window?.decorView?.setPadding(context.dp2px(left), 0, context.dp2px(right), 0) //消除边框
            //            window?.setWindowAnimations(R.style.Animation_CustomPopup)
            setCanceledOnTouchOutside(true)
            setCancelable(true)
            window?.setGravity(gravity)
        }
        return dialog
    }
}